package com.lh.web.service.impl.shop;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.common.entity.admin.BannerShop;
import com.lh.common.entity.admin.Shop;
import com.lh.common.utils.PageUtils;
import com.lh.web.dao.admin.ShopMapper;
import com.lh.web.service.serviceAdmin.shop.ShopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author FH
 * @ClassName: ShopServiceImpl
 * @Description: (类作用)
 * @date: 2020-05-12 14:57
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Override
    public PageUtils queryShopList(Map<String, Object> param, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Shop> notices = this.baseMapper.queryShopList(param);
        for (Shop notice:notices){
            String sortIds = notice.getSortIds();
            if(sortIds != null){
                String[] arr =sortIds.split(",");
                String sortName = "";
                for (int i=0; i < arr.length; i++){
                    Map map = this.baseMapper.querySortName(arr[i]);
                    sortName = sortName + map.get("sort_name").toString() + "/";
                }
                sortName = sortName.substring(0, sortName.length() -1);
                notice.setSortNames(sortName);
            }
        }
        return new PageUtils(notices);
    }

    @Override
    public PageUtils getBannerShopList(Map<String, Object> param, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<BannerShop> notices = this.baseMapper.getBannerShopList(param);
        return new PageUtils(notices);
    }

    @Override
    public Shop getShopDetail(Long shopId) {
        return this.baseMapper.getShopDetail(shopId);
    }

    @Override
    public Shop getCategoryName(String shopName) {
        return this.baseMapper.getCategoryName(shopName);
    }

    @Override
    public Shop updateShop(Shop shop) {
        return this.baseMapper.updateShop(shop);
    }

    @Override
    public int updateShopStatus(Map<String, Object> params) {
        return this.baseMapper.updateShopStatus(params);
    }

    @Override
    public int queryActivityShop(Long shopId) {
        return this.baseMapper.queryActivityShop(shopId);
    }

    @Override
    public int queryShop(Long shopId) {
        return this.baseMapper.queryShop(shopId);
    }

    @Override
    public int addShopSortRef(Long shopId, String sortIds) {
        String[] arr =sortIds.split(",");
        Long sortId = Long.parseLong(arr[arr.length-1]);
        return deleteShopSortRef(shopId,sortId);
    }

    @Override
    public int saveShop(Shop shop) {
        return this.baseMapper.saveShop(shop);
    }

    @Override
    @Transactional
    public int deleteShop(Long sid) {
        this.baseMapper.deleteShop(sid);
        this.baseMapper.deleteShopSortRef(sid);
        this.baseMapper.deleteShopColumnRef(sid);
        return 0;
    }


    public int deleteShopSortRef(Long shopId, Long sortId){
        //先删除关系
        this.baseMapper.deleteShopSortRef(shopId);

        //再保存关系
        this.baseMapper.addShopSortRef(shopId,sortId);
        return 0;
    }
}
